namespace Wolfpack.Contrib.Tests.Publishers.Deployment
{
    using Core.Testing.Bdd;
    using NUnit.Framework;
    using StoryQ;

    public class ExecuteDeploymentPublisherTests : BddFeature
    {
        protected override Feature DescribeFeature()
        {
            return new Story("Testing the execute deployment publisher")
                .InOrderTo("Ensure the component works correctly")
                .AsA("Downloaded executable package")
                .IWant("To inspect the commandline");
        }

        [Test]
        [Category("Unit")]
        public void PostDownloadRelativeCommand()
        {
            using (var domain = new ExecuteDeploymentPublisherDomain())
            {
                Feature.WithScenario("The command line is relative (not rooted)")
                    .Given(domain.TheCommandLine_, "bob.exe")
                        .And(domain.TheDownloadFolder, "TestDownload")
                        .And(domain.TheLauncherIndicatesSuccess)
                        .And(domain.TheAgentIsStarted)
                    .When(domain.ThePublisherIsCalled)
                    .Then(domain.TheLauncherWasCalled)
                        .And(domain.TheLaunchCommandLinePathShouldBeRooted)
                        .And(domain.TheLaunchCommandLinePathShouldEndWith_, @"\TestDownload\bob.exe")
                    .ExecuteWithReport();
            }
        }

        [Test]
        [Category("Unit")]
        public void PostDownloadAbsoluteCommand()
        {
            using (var domain = new ExecuteDeploymentPublisherDomain())
            {
                Feature.WithScenario("The command line is absolute")
                    .Given(domain.TheCommandLine_, @"z:\folder\bob.exe")
                        .And(domain.TheDownloadFolder, "TestDownload")
                        .And(domain.TheLauncherIndicatesSuccess)
                        .And(domain.TheAgentIsStarted)
                    .When(domain.ThePublisherIsCalled)
                    .Then(domain.TheLauncherWasCalled)
                        .And(domain.TheLaunchCommandLinePathShouldBeRooted)
                        .And(domain.TheLaunchCommandLinePathShould_, @"z:\folder\bob.exe")
                        .And(domain.ThereShouldBe_HealthCheckNotificationsReceived, 1)
                        .And(domain.TheNotificationRecievedAtIndex_ShouldHaveResult_, 1, true)
                        .And(domain.TheCheckNotificationTagShouldBeTheSameAsTheTriggerMessageTag)
                    .ExecuteWithReport();
            }
        }

        [Test]
        [Category("Unit")]
        public void PostDownloadFailedCommand()
        {
            using (var domain = new ExecuteDeploymentPublisherDomain())
            {
                Feature.WithScenario("The launched command failed")
                    .Given(domain.TheCommandLine_, @"z:\folder\bob.exe")
                        .And(domain.TheDownloadFolder, "TestDownload")
                        .And(domain.TheLauncherReturnsErrorCode_, 1)
                        .And(domain.TheAgentIsStarted)
                    .When(domain.ThePublisherIsCalled)
                    .Then(domain.ThereShouldBe_HealthCheckNotificationsReceived, 1)
                        .And(domain.TheNotificationRecievedAtIndex_ShouldHaveResult_, 1, false)
                        .And(domain.TheCheckNotificationShouldHaveProperty_WithValue_, "ErrorCode", "1")
                    .ExecuteWithReport();
            }
        }
    }
}